package cn.gok.homework;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/*
除了 全参数构造外，还包含 public void sell(){ //显示本书被卖了}
 private  void sell(float price,String person)://显示本书卖给了xxx，卖了 xxx元
通过反射实现：
1 获取Book类信息，并获取全属性构造函数对象constructor
2 利用该构造对象，创建1个书的实例。
3 通过反射，分别获取两个sell方法对应的method对象，
并破坏封装性，分别调用该方法。
 */
class Book {
    private int bid;
    private String name;
    private float price;

    public Book(int bid, String name, float price) {
        this.bid = bid;
        this.name = name;
        this.price = price;
    }

    public int getBid() {
        return bid;
    }

    public void setBid(int bid) {
        this.bid = bid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }
    public void sell(){
        System.out.println(this.getName()+"被卖了");
    }
    private  void sell(float price,String person){
        System.out.println(this.getName()+"卖给了"+person+"，卖了"+price+"元");
    }

    @Override
    public String toString() {
        return "Book{" +
                "bid=" + bid +
                ", name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

public class Homework {
    public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, ClassNotFoundException, InvocationTargetException {
        Class book = Class.forName("cn.gok.homework.Book");
        Constructor con = book.getConstructor(int.class,String.class,float.class);
        Book book1 = (Book) con.newInstance(101,"西游记",100f);
        System.out.println(book1);

        Method m = book.getDeclaredMethod("sell",null);
        m.setAccessible(true);
        m.invoke(book1,null);
        Method m1 = book.getDeclaredMethod("sell",float.class,String.class);
        m1.setAccessible(true);
        m1.invoke(book1,100f,"周杰伦");
    }
}
